﻿using ERP.ERPModels.Production.MasterData;
using ERP.ERPModels.Production.ProductionManagement;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.ProductionManage.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ProductionManage
{
    public class ProductionDispatchService: ApplicationService, IProductionDispatchService
    {
        private readonly IRepository<ProductionDispatch> _productionDispatchRepository;

        public ProductionDispatchService(IRepository<ProductionDispatch> productionDispatchRepository)
        {
            _productionDispatchRepository = productionDispatchRepository;
        }

        /// <summary>
        /// 更新生产排产
        /// </summary>
        /// <param name="param">更新参数</param>
        /// <returns></returns>
        public async Task<int> UpdateProductionDispatch(ProductionDispatchUpdateDTO param)
        {
            // 查找现有记录
            var existingDispatch = await _productionDispatchRepository.FindAsync(e=>e.Id == param.Id);
            if (existingDispatch == null)
            {
                return 0;
            }

            // 更新字段
            //ObjectMapper.Map(param, existingDispatch);
            existingDispatch.UpdateTime = DateTime.Now;
            existingDispatch.Assignee = param.Assignee;
            
            // 保存更新
            await _productionDispatchRepository.UpdateAsync(existingDispatch);
            return 1;
        }

        /// <summary>
        /// 删除生产排产
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteProductionDispatch(int Id)
        {
            try
            {
                var result = await _productionDispatchRepository.FindAsync(x => x.Id == Id);
                if (result == null)
                {
                    return 0;
                }
                await _productionDispatchRepository.DeleteAsync(result);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
    }
}
